package com.unitylib.alarmnotifyer;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

public class AlarmReceiver extends BroadcastReceiver {
    private String notifyReceiverPackageIdentity = "com.playlib.alarmnotify.alarm_notification";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("AlarmReceiver", "ReceiveBroad::Receive");
        if(intent.getAction().equals(notifyReceiverPackageIdentity)){
            String title = intent.getStringExtra("title");
            String textContent = intent.getStringExtra("textContent");
            String notifyChannelId = intent.getStringExtra("notifyChannelId");
            //不用AlarmNotifyer里的是因为还没把alarmnotifyer里面的unityactivity抽象成content参数传入，在receiver激活时UnityPlayer.currentActivity是空
            SendNotify(notifyChannelId, title, textContent, context);
        }
    }

    public void SendNotify(String notifyChannelId, String title, String textContent, Context context){
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(new ComponentName(context, UnityPlayerActivity.class));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        //设置一个延时Intent，会把当前的意图进行封装保留，保证在当前app关闭后也能执行
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            //设置消息的发布通道，这个是安卓新版本的功能，用来统一控制消息的管道
            NotificationChannel channel = new NotificationChannel(notifyChannelId, notifyChannelId,
                    NotificationManager.IMPORTANCE_DEFAULT); //新建管道，并且设置消息优先级
            channel.setDescription(notifyChannelId); //做些信息配置
            channel.enableLights(true);
            channel.setSound(null, null);

            //获取消息通知中心的对象
            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            //要求消息通知中心创建这个管道
            notificationManager.createNotificationChannel(channel);
            //创建一个消息的创建者
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel.getId());
            builder.setSmallIcon(R.drawable.notify_icon)
                    .setContentTitle(title)
                    .setContentText(textContent)
                    .setSmallIcon(R.drawable.notify_icon)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(textContent))
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);

            //将消息创建者创建的消息发布到消息通知中心
            notificationManager.notify(notifyChannelId.hashCode(), builder.build());
        }else{
            Notification.Builder builder = new Notification.Builder(context)
                    .setContentTitle(title)
                    .setContentText(textContent)
                    .setSmallIcon(R.drawable.notify_icon)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setStyle(new Notification.BigTextStyle().bigText(textContent))
                    .setPriority(Notification.PRIORITY_DEFAULT);
            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(notifyChannelId.hashCode(), builder.build());
        }
    }
}
